package cn.le.aopAnnotationDataSource.annotutil;

import cn.le.aopAnnotationDataSource.annotation.IsEmpty;
import cn.le.aopAnnotationDataSource.annotation.MaxSize;
import cn.le.aopAnnotationDataSource.annotation.MinSize;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by admin on 2017/3/24.
 * 验证注解处理类
 */

public class VerificationUtil {

    public static void validate(Object bean)  {
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("message","验证通过");
        result.put("result",true);

        Class<?> cls = bean.getClass();
        //检测filed是否存在
        //获取实体类字段集合
        Field[] fields = cls.getDeclaredFields();
        for (Field field : fields)
        {
            //通过反射获取该属性对应的值
            field.setAccessible(true);
            //获取字段值
            try {
                Object value = field.get(bean);
                //获取字段上的注解集合
                Annotation[] arraytation = field.getAnnotations();
                for (Annotation annotation : arraytation) {
                    //获取注解类型(注解类的class)
                    Class<?> clsa = annotation.annotationType();
                    //获取注解类中的方法集合
                    Method[] methodArray = clsa.getDeclaredMethods();
                    for (Method method: methodArray) {
                        //获取方法名
                        String methodName = method.getName();
                        //获取错误提示方式的调用
                        if (methodName.equals("message"))
                        {
                            continue;
                        }
                        //初始化注解验证的方法处理类
                        Object object = VerificationUtil.class.newInstance();
                        //根据方法名,获取方法
                        Method am = object.getClass().getDeclaredMethod(methodName,Object.class,Field.class);
                        //调用方法
                        result = (Map<String, Object>) am.invoke(object,value,field);
                        //验证结果,有一处失败则退出
                        if (result.get("result").equals(false))
                        {
                            throw new IllegalArgumentException(result.toString());
                        }
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }

        }
    }

    /**
     * 验证是否为空
     * @param value
     * @param field
     * @return
     */
    public Map<String,Object> isEmpty(Object value,Field field)
    {
        Map<String,Object> result = new HashMap<String,Object>();
        IsEmpty isEmpty = field.getAnnotation(IsEmpty.class);
        if (value == null || value.equals(""))
        {
            result.put("message",field.getName()+isEmpty.message());
            result.put("result",false);
        }
        else
        {
            result.put("message","验证通过");
            result.put("result",true);
        }
        return result;
    }

    /**
     * 验证最长度
     * @param value
     * @param field
     * @return
     */
    public Map<String,Object> max(Object value,Field field)
    {
        Map<String,Object> result = new HashMap<String,Object>();
        MaxSize maxSize = field.getAnnotation(MaxSize.class);

        if (value != null && value.toString().length() > maxSize.max() )
        {
            result.put("message",field.getName()+maxSize.message());
            result.put("result",false);
        }
        else
        {
            result.put("message","验证通过");
            result.put("result",true);
        }
        return result;
    }

    /**
     * 验证最小长度
     * @param value
     * @param field
     * @return
     */
    public Map<String,Object> min(Object value,Field field)
    {
        Map<String,Object> result = new HashMap<String,Object>();
        MinSize minSize = field.getAnnotation(MinSize.class);

        if (value != value && value.toString().length() < minSize.min())
        {
            result.put("message",field.getName()+minSize.message());
            result.put("result",false);
        }
        else
        {
            result.put("message","验证通过");
            result.put("result",true);
        }
        return result;
    }

}
